﻿using System;
using FreeSwitch.EventSocket.Parser;

namespace FreeSwitch.EventSocket.Commands
{
	/// <summary>
	/// Send a command.
	/// </summary>
	public class Api : ICommand
	{
		private readonly string[] _arguments;
		private readonly string _command;

		/// <summary>
		/// Initializes a new instance of the <see cref="Api"/> class.
		/// </summary>
		/// <param name="command">Name of command to execute</param>
		/// <param name="arguments">The arguments.</param>
		public Api(string command, params string[] arguments)
		{
			_command = command;
			_arguments = arguments;
		}

		#region ICommand Members

		/// <summary>
		/// Parses a reply sent from FreeSwitch for a command.
		/// </summary>
		/// <param name="msg"></param>
		/// <returns></returns>
		public virtual ICommandReply ParseReply(ParsedMessage msg)
		{
			return CommandReply.Parse(msg);
		}

		/// <summary>
		/// Generate a string that can be sent to FreeSWITCH
		/// </summary>
		/// <returns>String that FreeSWITCH can interpret as a command.</returns>
		public string ToCommandString()
		{
			return ToString();
		}

		#endregion

		public override string ToString()
		{
			return string.Format("api {0} {1}", _command, string.Join(" ", _arguments));
		}
	}
}